package com.gmq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmq.common.CustomExcetion;
import com.gmq.common.R;
import com.gmq.dto.DishDto;
import com.gmq.entity.Category;
import com.gmq.entity.Dish;
import com.gmq.entity.DishFlavor;
import com.gmq.service.CategoryService;
import com.gmq.service.DishFlavorService;
import com.gmq.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @GetMapping("/page")
    public R<Page> dishPage(int page, int pageSize, String name) {
        log.info("查询菜品列表{},{}", page, pageSize);
        Page<Dish> page1 = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(page1, queryWrapper);
        //把数据拷贝到dishDto对象中
        BeanUtils.copyProperties(page1, dishDtoPage, "records");
        List<Dish> records = page1.getRecords();
        List<DishDto> list = new ArrayList<>();
        //获取分类名称
        for (Dish record : records) {
            DishDto dishDto = new DishDto();
            //拷贝普通属性
            BeanUtils.copyProperties(record, dishDto);
            Long categoryId = record.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if (byId != null) {
                String name1 = byId.getName();
                dishDto.setCategoryName(name1);
            }
            list.add(dishDto);
        }
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishListCache", allEntries = true)
    public R<String> status(@PathVariable int status, Long[] ids) {
        log.info("修改{}菜品状态{}", ids, status);
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        }
        return R.success("修改成功");
    }

    @DeleteMapping
    @Transactional
    @CacheEvict(cacheNames = "dishListCache", allEntries = true)
    public R<String> delete(Long[] ids) {
        log.info("删除菜品{}", ids);
        for (Long id : ids) {
            Dish byId = dishService.getById(id);
            if (byId.getStatus() == 1) {
                throw new CustomExcetion("无法删除还在售卖的菜品");
            }
            dishService.removeById(id);
        }
        return R.success("删除成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> dishItem(@PathVariable Long id) {
        DishDto dishDto = dishService.dishItem(id);
        return R.success(dishDto);
    }

    @PutMapping
    @CacheEvict(cacheNames = "dishListCache", key = "#dishDto.categoryId")
    public R<String> update(@RequestBody DishDto dishDto) {
        log.info("修改菜品{}", dishDto.toString());
        dishService.updateDish(dishDto);
        return R.success("修改成功");
    }

    @PostMapping
    @CacheEvict(cacheNames = "dishListCache", key = "#dishDto.categoryId")
    public R<String> add(@RequestBody DishDto dishDto) {
        log.info("新增菜品{}", dishDto.toString());
        dishService.addDish(dishDto);
        return R.success("新增成功");
    }

    @GetMapping("/list")
    @Cacheable(cacheNames = "dishListCache", key = "#categoryId")
    public R<List<DishDto>> list(Long categoryId) {
        List<DishDto> dishDtos = new ArrayList<>();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Dish::getCategoryId, categoryId);
        queryWrapper.like(Dish::getStatus, 1);
        List<Dish> list = dishService.list(queryWrapper);
        for (Dish dish : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //查询口味信息存入
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.like(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> list1 = dishFlavorService.list(queryWrapper1);
            dishDto.setFlavors(list1);
            dishDtos.add(dishDto);
        }
        return R.success(dishDtos);
    }

}
